<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport( 'joomla.application.component.controller' );

class work_pointController extends JController 
{
    
    function __construct( $default = array())	
    {
            parent::__construct( $default );
	}
    
    
    function report($tpl = null)
    {
        $date = date('Y-m-d');
/*
        
        
        
        //thong ke theo thang
        //truong hop di tre
        if($_SESSION['id_a_b']==0)
        {
            $sql = "select pf.id_employee, us.name, ts.Time, ws.intime, us.id, ab.id_a_b, ts.type_in_out, ts.id
                                FROM #__erp_employee_profile pf
                                LEFT JOIN #__erp_agent_branch_user ab
        	                    ON pf.id = ab.id_user
                                INNER JOIN #__users us
                                on pf.id = us.id
                                INNER JOIN #__erp_timesheet ts
                                on pf.id = ts.id_user
                                INNER JOIN #__erp_employee_workshifts ws
                                ON ws.id = pf.id_work_shifts  
                                Where month(ts.Time) = '".$post['thang']."'
                                and ts.type_in_out = 0
                                order by us.id , ts.time 
                            ";
            $db->setQuery($sql);
        }
        else
        {
            $sql = "select pf.id_employee, us.name, ts.Time, ws.intime, us.id, ab.id_a_b, ts.type_in_out, ts.id
                                FROM #__erp_employee_profile pf
                                LEFT JOIN #__erp_agent_branch_user ab
        	                    ON pf.id = ab.id_user
                                INNER JOIN #__users us
                                on pf.id = us.id
                                INNER JOIN #__erp_timesheet ts
                                on pf.id = ts.id_user
                                INNER JOIN #__erp_employee_workshifts ws
                                ON ws.id = pf.id_work_shifts  
                                Where month(ts.Time) = '".$post['thang']."'
                                and ab.id_a_b = '".$_SESSION['id_a_b']."'
                                and ts.type_in_out = 0
                                order by us.id , ts.time 
                            ";
            $db->setQuery($sql);
        }
        
        //truong hop ve som
        if($_SESSION['id_a_b']==0)
        {
            $sql = "select pf.id_employee, us.name, ts.Time, ws.intime, us.id, ab.id_a_b, ts.type_in_out, ts.id
                                FROM #__erp_employee_profile pf
                                LEFT JOIN #__erp_agent_branch_user ab
        	                    ON pf.id = ab.id_user
                                INNER JOIN #__users us
                                on pf.id = us.id
                                INNER JOIN #__erp_timesheet ts
                                on pf.id = ts.id_user
                                INNER JOIN #__erp_employee_workshifts ws
                                ON ws.id = pf.id_work_shifts  
                                Where month(ts.Time) = '".$post['thang']."'
                                and ts.type_in_out = 1
                                order by us.id desc, ts.time desc
                            ";
            $db->setQuery($sql);
        }
        else
        {
            $sql = "select pf.id_employee, us.name, ts.Time, ws.intime, us.id, ab.id_a_b, ts.type_in_out, ts.id
                                FROM #__erp_employee_profile pf
                                LEFT JOIN #__erp_agent_branch_user ab
        	                    ON pf.id = ab.id_user
                                INNER JOIN #__users us
                                on pf.id = us.id
                                INNER JOIN #__erp_timesheet ts
                                on pf.id = ts.id_user
                                INNER JOIN #__erp_employee_workshifts ws
                                ON ws.id = pf.id_work_shifts  
                                 Where month(ts.Time) = '".$post['thang']."'
                                and ab.id_a_b = '".$_SESSION['id_a_b']."'
                                and ts.type_in_out = 1
                                order by us.id desc, ts.time desc
                            ";
            $db->setQuery($sql);
        }*/
        
        JRequest::setVar('view', 'report');
		parent::display($tpl);    
    }
    

        
    function save()
    {
        $date = date('Y-m-d H:i:s');
        $db = JFactory::getDBO();
        $model = $this->getModel('function_library');
        $post = JRequest::get('post',4);
        print_r($post);
        //kiem tra xem ma nv co ton tai hay khong 
        if($model->check_emplyee($post['manv'])==0)
        {
            $this->setRedirect("index.php?option=com_work_point","Sai ma nhan vien","error");
            return;
        }
        else
        {
            //kiem tra tinh trang thai cuoi cung cua nhan vien, neu la ra thi khong cho ra. neu la vao thi khong cho vao
            $sql = "select * from #__erp_timesheet where id_user='".$post['id_use']."' order by id desc";
            $db->setQuery($sql);//print_r($db);
            $data= $db->loadObject();
            if($data->type_in_out == $post['action'] && $post['action']==0)
            {
                $this->setRedirect("index.php?option=com_work_point","Nhan vien nay da vao cong ty.","error");
                return;
            }
            if($data->type_in_out == $post['action'] && $post['action']==1)
            {
                $this->setRedirect("index.php?option=com_work_point","Nhan vien nay da roi khoi cong ty.","error");
                return;
            }
            $row =  & JTable::getInstance('erp_timesheet', 'Table');
            $row->id_user = $post['id_use'];
            $row->Time = $date;
            $row->type_in_out = $post['action'];
            if (!$row->store()) { //thuc thi them record  neu co loi se hien thi
                              	     JError::raiseError(500, $row->getError());
                                       }
            $this->setRedirect("index.php?option=com_work_point","Thanh Cong.");
            return;                         
        }
        
        
        
        
    }     
    
	function display($tpl = null) 
        {    
        	//Set the default view, just in case
		$view = JRequest::getCmd('view');
		if(empty($view)) {
                    JRequest::setVar('view', 'dashboard');
		};
		parent::display($tpl);
	}
        
        function info_employee2()
        {
             $db = JFactory::getDBO();
             $manv = $_POST['manv'];
             //echo $manv;die;
             $sql = "SELECT #__erp_employee_profile.*, #__users.name
                        FROM #__erp_employee_profile, #__users
                        WHERE #__erp_employee_profile.id = #__users.id
                        AND #__erp_employee_profile.id_employee = '".$manv."'";
             $db->setQuery($sql);$data= $db->loadObject();
             $cup = array('id_em' => $data->id_employee
                                    ,'name' =>$data->name
                                        ,'img' => $data->photo
                                        ,'id'=>$data->id);
                                    $x = json_encode($cup);
                                    echo $x;
                                    die;
        }
        
        function info_employee()
        {
            //echo $_POST['manv'];
            $db = JFactory::getDBO();
            $manv = $_POST['manv'];
            if(isset($_POST['manv'])) 
            {
                if($_SESSION['id_a_b']==0)//neu o tru so chinh
		{
                        $sql = "SELECT #__erp_employee_profile.*, #__erp_agent_branch_user.id_a_b, #__users.name
                        FROM #__erp_employee_profile
                                LEFT JOIN #__erp_agent_branch_user
                        	ON #__erp_employee_profile.id = #__erp_agent_branch_user.id_user
                                INNER JOIN #__users
                                on #__users.id = #__erp_employee_profile.id
                        WHERE #__erp_employee_profile.id_employee LIKE '$manv%'";
                        
                        $db->setQuery($sql);
                        //print_r($db);die;
                        $data= $db->loadObjectList();
                        foreach($data as $row)
                        {
                            if($row->id_a_b == null)
                                {
                                    echo '<li onClick="fill(\''.$row->id_employee.'\');">'.$row->id_employee."-".$row->name.'</li>';           
                                }   
                        }                    
		}
                else //nguoc lai
                    {
                        $sql = "SELECT #__erp_employee_profile.*, #__erp_agent_branch_user.id_a_b, #__users.name
                        FROM #__erp_employee_profile
                                LEFT JOIN #__erp_agent_branch_user
                        	ON #__erp_employee_profile.id = #__erp_agent_branch_user.id_user
                                INNER JOIN #__users
                                on #__users.id = #__erp_employee_profile.id
                        WHERE #__erp_agent_branch_user.id_a_b = '".$_SESSION['id_a_b']."'
                        AND #__erp_employee_profile.id_employee LIKE '$manv%' 
                            ";
                        $db->setQuery($sql);
                        //print_r($db);die;
                        $data= $db->loadObjectList();
                        foreach($data as $row)
                        {
                            echo '<li onClick="fill(\''.$row->id_employee.'\');">'.$row->id_employee."-".$row->name.'</li>';        
                        }   
                    }
            }
            die;
        }
}
?>




